<?php

namespace iBrand\Store\Backend\Console;

use Illuminate\Console\Command;
use DB;

class InitMenusCommand extends Command
{
	protected $signature = 'import:init-backend-menus';

	protected $description = 'import init backend menus';

	public function handle()
	{
		$menu = config('admin.database.menu_model');
		$menu::truncate();

		$lastOrder    = DB::table(config('admin.database.menu_table'))->max('order');
		$systemManage = DB::table(config('admin.database.menu_table'))->where('title', '系统管理')->where('parent_id', 0)->first();
		if (!$systemManage) {
			$systemManageId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => 0,
				'order'      => ++$lastOrder,
				'title'      => '系统管理',
				'icon'       => 'fa-tasks',
				'blank'      => 0,
				'uri'        => '/',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$systemManageId = $systemManage->id;
		}

		$index = DB::table(config('admin.database.menu_table'))->where('title', 'Index')->where('parent_id', $systemManageId)->first();
		if (!$index) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $systemManageId,
				'order'      => ++$lastOrder,
				'title'      => 'Index',
				'icon'       => 'fa-bar-chart',
				'blank'      => 0,
				'uri'        => '/',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$admin = DB::table(config('admin.database.menu_table'))->where('title', 'Admin')->where('parent_id', $systemManageId)->first();
		if (!$admin) {
			$adminId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $systemManageId,
				'order'      => ++$lastOrder,
				'title'      => 'Admin',
				'icon'       => 'fa-tasks',
				'blank'      => 0,
				'uri'        => '',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$adminId = $admin->id;
		}

		$users = DB::table(config('admin.database.menu_table'))->where('title', 'Users')->where('parent_id', $adminId)->first();
		if (!$users) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $adminId,
				'order'      => ++$lastOrder,
				'title'      => 'Users',
				'icon'       => 'fa-users',
				'blank'      => 0,
				'uri'        => 'auth/users',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$roles = DB::table(config('admin.database.menu_table'))->where('title', 'Roles')->where('parent_id', $adminId)->first();
		if (!$roles) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $adminId,
				'order'      => ++$lastOrder,
				'title'      => 'Roles',
				'icon'       => 'fa-user',
				'blank'      => 0,
				'uri'        => 'auth/roles',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$permission = DB::table(config('admin.database.menu_table'))->where('title', 'Permission')->where('parent_id', $adminId)->first();
		if (!$permission) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $adminId,
				'order'      => ++$lastOrder,
				'title'      => 'Permission',
				'icon'       => 'fa-ban',
				'blank'      => 0,
				'uri'        => 'auth/permissions',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$menu = DB::table(config('admin.database.menu_table'))->where('title', 'Menu')->where('parent_id', $adminId)->first();
		if (!$menu) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $adminId,
				'order'      => ++$lastOrder,
				'title'      => 'Menu',
				'icon'       => 'fa-bars',
				'blank'      => 0,
				'uri'        => 'auth/menu',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$operationLog = DB::table(config('admin.database.menu_table'))->where('title', 'Operation log')->where('parent_id', $adminId)->first();
		if (!$operationLog) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $adminId,
				'order'      => ++$lastOrder,
				'title'      => 'Operation log',
				'icon'       => 'fa-history',
				'blank'      => 0,
				'uri'        => 'auth/logs',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$helpers = DB::table(config('admin.database.menu_table'))->where('title', 'Helpers')->where('parent_id', $systemManageId)->first();
		if (!$helpers) {
			$helpersId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $systemManageId,
				'order'      => ++$lastOrder,
				'title'      => 'Helpers',
				'icon'       => 'fa-gears',
				'blank'      => 0,
				'uri'        => '',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$helpersId = $helpers->id;
		}

		$scaffold = DB::table(config('admin.database.menu_table'))->where('title', 'Scaffold')->where('parent_id', $helpersId)->first();
		if (!$scaffold) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $helpersId,
				'order'      => ++$lastOrder,
				'title'      => 'Scaffold',
				'icon'       => 'fa-keyboard-o',
				'blank'      => 0,
				'uri'        => 'helpers/scaffold',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$databaseTerminal = DB::table(config('admin.database.menu_table'))->where('title', 'Database terminal')->where('parent_id', $helpersId)->first();
		if (!$databaseTerminal) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $helpersId,
				'order'      => ++$lastOrder,
				'title'      => 'Database terminal',
				'icon'       => 'fa-database',
				'blank'      => 0,
				'uri'        => 'helpers/terminal/database',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$artisan = DB::table(config('admin.database.menu_table'))->where('title', 'Laravel artisan')->where('parent_id', $helpersId)->first();
		if (!$artisan) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $helpersId,
				'order'      => ++$lastOrder,
				'title'      => 'Laravel artisan',
				'icon'       => 'fa-terminal',
				'blank'      => 0,
				'uri'        => 'helpers/terminal/artisan',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$routes = DB::table(config('admin.database.menu_table'))->where('title', 'Routes')->where('parent_id', $helpersId)->first();
		if (!$routes) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $helpersId,
				'order'      => ++$lastOrder,
				'title'      => 'Routes',
				'icon'       => 'fa-list-alt',
				'blank'      => 0,
				'uri'        => 'helpers/routes',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$redisManager = DB::table(config('admin.database.menu_table'))->where('title', 'Redis manager')->where('parent_id', $systemManageId)->first();
		if (!$redisManager) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $systemManageId,
				'order'      => ++$lastOrder,
				'title'      => 'Redis manager',
				'icon'       => 'fa-database',
				'blank'      => 0,
				'uri'        => 'redis',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$backup = DB::table(config('admin.database.menu_table'))->where('title', 'Backup')->where('parent_id', $systemManageId)->first();
		if (!$backup) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $systemManageId,
				'order'      => ++$lastOrder,
				'title'      => 'Backup',
				'icon'       => 'fa-copy',
				'blank'      => 0,
				'uri'        => 'backup',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$logViewer = DB::table(config('admin.database.menu_table'))->where('title', 'Log viewer')->where('parent_id', $systemManageId)->first();
		if (!$logViewer) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $systemManageId,
				'order'      => ++$lastOrder,
				'title'      => 'Log viewer',
				'icon'       => 'fa-database',
				'blank'      => 0,
				'uri'        => 'logs',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$scheduling = DB::table(config('admin.database.menu_table'))->where('title', 'Scheduling')->where('parent_id', $systemManageId)->first();
		if (!$scheduling) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $systemManageId,
				'order'      => ++$lastOrder,
				'title'      => 'Scheduling',
				'icon'       => 'fa-clock-o',
				'blank'      => 0,
				'uri'        => 'scheduling',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}
	}
}